package com.gao.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import com.gao.dao.CartDao;
import com.gao.domain.Cart;
import com.gao.domain.Product;
import com.gao.domain.User;
import com.gao.util.DBUtil;

public class CartDaoImpl implements CartDao{

	@Override
	public boolean insert(Cart cart) throws SQLException {
		Connection conn = DBUtil.getConnection();
		String sql = "insert into cart values(null,?,?,?)";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setInt(1, cart.getProduct().getPid());
		pstmt.setInt(2, cart.getCartNum());
		pstmt.setInt(3, cart.getUser().getUid());
		int num = pstmt.executeUpdate();
		DBUtil.release(pstmt, conn);
		return num>0;
	}

	@Override
	public List<Cart> getCartsByUid(int uid)  throws SQLException{
		List<Cart> carts = new ArrayList<Cart>();
		Cart cart = null;
		Connection conn = DBUtil.getConnection();
		String sql = "select * from cart where uid=?";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setInt(1, uid);
		ResultSet rs = pstmt.executeQuery();
		while(rs.next()){
			cart = new Cart(rs.getInt(1),new Product(rs.getInt(2)),rs.getInt(3),new User(rs.getInt(4)));		
			carts.add(cart);
		}
		DBUtil.release(rs,pstmt, conn);
		return carts;
	}

	@Override
	public boolean deleteByCid(int cid)  throws SQLException{
		// TODO Auto-generated method stub
		Connection conn = DBUtil.getConnection();
		String sql = "delete from cart where cid=?";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setInt(1, cid);
		int num = pstmt.executeUpdate();
		DBUtil.release(pstmt, conn);
		return num>0;
	}

	@Override
	public Cart get(int pid, int uid)  throws SQLException{
		Cart cart = null;
		Connection conn = DBUtil.getConnection();
		String sql = "select * from cart where pid=? and uid=?";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setInt(1, pid);
		pstmt.setInt(2, uid);
		ResultSet rs = pstmt.executeQuery();
		if(rs.next()){
			cart = new Cart(rs.getInt(1),new Product(rs.getInt(2)),rs.getInt(3),new User(rs.getInt(4)));
		}
		DBUtil.release(rs,pstmt, conn);
		return cart;
	}

	@Override
	public boolean updateNumber(int increment, int pid, int uid) throws SQLException {
		Connection conn = DBUtil.getConnection();
		String sql = "update cart set cart_num=cart_num+? where pid=? and uid=?";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setInt(1, increment);
		pstmt.setInt(2, pid);
		pstmt.setInt(3, uid);
		int num = pstmt.executeUpdate();
		DBUtil.release(pstmt, conn);
		return num>0;
	}

	@Override
	public Cart getByCid(int cid)  throws SQLException{
		Cart cart = null;
		Connection conn = DBUtil.getConnection();
		String sql = "select * from cart where cid=?";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setInt(1, cid);
		ResultSet rs = pstmt.executeQuery();
		if(rs.next()){
			cart = new Cart(rs.getInt(1),new Product(rs.getInt(2)),rs.getInt(3),new User(rs.getInt(4)));
		}
		DBUtil.release(rs,pstmt, conn);
		return cart;
	}

}
